package com.wsb.dao;

import com.wsb.entry.SubmsUser;

import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SmbmsUserDao {
    public List<SubmsUser> getSmbmUserByUserAll() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //第二步 获得连接
            String url = "jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8";
            String userRoot = "root";
            String userPwd = "wxok";
            Connection connection = DriverManager.getConnection(url, userRoot, userPwd);
            String sql = "select * from smbms_user ";
            PreparedStatement pst = connection.prepareStatement(sql);
            ResultSet resultSet = pst.executeQuery();
            List<SubmsUser> smbmsUserList  = new ArrayList<>();
            //取数据库当中的数据赋值 smbmsUser
            while (resultSet.next()){
                SubmsUser  smbmsUser = new SubmsUser();
                String userCode =    resultSet.getString("userCode");
                String userName =     resultSet.getString("userName");
                String userPassword =     resultSet.getString("userPassword");
                //重数据库里查出的数据，赋值Java类对象
                smbmsUser.setUserCode(userCode);
                smbmsUser.setUserName(userName);
                smbmsUser.setUserPassword(userPassword);
                smbmsUserList.add(smbmsUser);
            }

          return smbmsUserList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public SubmsUser getSmbmUserByUserName(String UserCodeParam) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8";
            String userRoot = "root";
            String userPwd = "123456";
            Connection connection = DriverManager.getConnection(url, userRoot, userPwd);
            String sql = "select * from smbms_user where userCode = '" +UserCodeParam+ "'";
            PreparedStatement pst = connection.prepareStatement(sql);
            ResultSet resultSet = pst.executeQuery();
            SubmsUser smbmsUser = null;
            while (resultSet.next()) {
                smbmsUser = new SubmsUser();
                String userCode = resultSet.getString("userCode");
                String userName = resultSet.getString("userName");
                String userPassword = resultSet.getString("userPassword");
                smbmsUser.setUserCode(userCode);
                smbmsUser.setUserName(userName);
                smbmsUser.setUserPassword(userPassword);
            }
            return smbmsUser;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}